package com.gr.springboot_shiro.realm;

import com.gr.springboot_shiro.config.MyApplicationContext;
import com.gr.springboot_shiro.entity.Perms;
import com.gr.springboot_shiro.entity.User;
import com.gr.springboot_shiro.service.UserService;
import com.gr.springboot_shiro.utils.MyByteSource;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 自定义realm
 *
 * @author Katakuri
 */

public class MyRealm extends AuthorizingRealm {

    public MyRealm() {
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        System.out.println("开始认证账号 = " + principal);
        UserService userService = (UserService) MyApplicationContext.getBean("userService");
        //System.out.println("userService = " + userService);
        //通过username查询数据库  获取到user对象
        User user = userService.findByUserName(principal);
        System.out.println("user = " + user);
        if (!ObjectUtils.isEmpty(user)) {
            return new SimpleAuthenticationInfo(principal
                    , user.getPassword(),
                    new MyByteSource(user.getSalt()),
                    this.getName());
        }

        return null;
    }


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("进行自定义授权  principals = " + principals);
        String username = (String) principals.getPrimaryPrincipal();
        UserService userService = (UserService) MyApplicationContext.getBean("userService");
        //通过数据库查询角色
        User user = userService.findRolesByUserName(username);
        if (!CollectionUtils.isEmpty(user.getRoles())) {
            SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
            //给user添加权限
            user.getRoles().forEach(role -> {
                simpleAuthorizationInfo.addRole(role.getName());
                List<Perms> perms = userService.findPermsByRoleId(role.getId());
                if (!CollectionUtils.isEmpty(perms)) {
                    //添加权限
                    perms.forEach(perm -> {
                        System.out.println("给"+role.getName()+" 添加权限： = " + perm.getName());
                        simpleAuthorizationInfo.addStringPermission(perm.getName());
                    });
                }
            });

            return simpleAuthorizationInfo;

        }


        //if ("dada".equals(username)){
        //    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //    simpleAuthorizationInfo.addRole("admin");
        //    simpleAuthorizationInfo.addRole("user");
        //    //增加添加权限
        //    simpleAuthorizationInfo.addStringPermission("user:create:*");
        //    //增加修改权限
        //    simpleAuthorizationInfo.addStringPermission("user:update:*");
        //    simpleAuthorizationInfo.addStringPermission("user:delete:*");
        //return simpleAuthorizationInfo;
        //}

        //增加权限控制
        //simpleAuthorizationInfo.addStringPermission("product:create;02");

        return null;
    }
}
